<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">

          <a-col :md="6" :sm="12">
            <a-form-item label="账号">
              <!--<a-input placeholder="请输入账号查询" v-model="queryParam.username"></a-input>-->
              <j-input placeholder="输入账号模糊查询" v-model="queryParam.username"></j-input>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="8">
            <a-form-item label="性别">
              <a-select v-model="queryParam.sex" placeholder="请选择性别">
                <a-select-option value="">请选择</a-select-option>
                <a-select-option value="1">男</a-select-option>
                <a-select-option value="2">女</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>


          <template v-if="toggleSearchStatus">
            <a-col :md="6" :sm="8">
              <a-form-item label="真实名字">
                <a-input placeholder="请输入真实名字" v-model="queryParam.realname"></a-input>
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="8">
              <a-form-item label="手机号码">
                <a-input placeholder="请输入手机号码查询" v-model="queryParam.phone"></a-input>
              </a-form-item>
            </a-col>

            <a-col :md="6" :sm="8">
              <a-form-item label="用户状态">
                <a-select v-model="queryParam.status" placeholder="请选择">
                  <a-select-option value="">请选择</a-select-option>
                  <a-select-option value="1">正常</a-select-option>
                  <a-select-option value="2">冻结</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>

          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator" style="border-top: 5px">
      <a-button @click="handleAdd" type="primary" icon="plus">添加用户</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">批量导入用户</a-button>
      </a-upload>
      <a-button type="primary" icon="download" @click="handleExportXls('用户信息')">导出系统角色</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('用户信息')">导出系统用户角色</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        bordered
        size="middle"
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :scroll="{ x: 1500, y: 300 }"
        @change="handleTableChange">

        <span slot="sexSlot" slot-scope="text, record">
          {{text=='0' ? '男' : text=='1' ? '女' : '其它'}}
        </span>
        <span slot="lockedSlot" slot-scope="text, record">
          {{text=='0' ? '正常' : text=='1' ? '锁定' : '其它'}}
        </span>

        <span slot="actionSlot" slot-scope="text, record">
          <a-tag color="green" style="cursor: pointer;" @click="handleEdit(record)">修改</a-tag>

          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a-tag style="cursor: pointer;"  color="pink">删除</a-tag>
          </a-popconfirm>
          
          <a-tag color="orange" style="cursor: pointer;"  @click="handleUserRole(record.id)">用户角色</a-tag>

          <a-tag color="orange" style="cursor: pointer;"  @click="handleUserPermission(record.id)">用户权限</a-tag>

          <a-tag color="orange" style="cursor: pointer;"  @click="handleChangePassword(record.username)">重置密码</a-tag>
        </span>


      </a-table>
    </div>
    <!-- table区域-end -->

    <user-modal ref="modalForm" @ok="modalFormOk"></user-modal>

    <password-modal ref="passwordmodal" @ok="passwordModalOk"></password-modal>

    <user-role-modal ref="modalUserRole"></user-role-modal>

    <user-permission-modal ref="userPermissionModal"></user-permission-modal>

  </a-card>
</template>

<script>
  import UserModal from './modules/UserModal'
  import PasswordModal from './modules/PasswordModal'
  import UserRoleModal from './modules/UserRoleModal'
  import UserPermissionModal from './modules/UserPermissionModal'
  import {putAction,getFileAccessHttpUrl,getAction} from '@/api/manage'
  import {UserListMixin} from '@/mixins/UserListMixin'
  import JInput from '@/components/jeecg/JInput'

  export default {
    name: "UserList",
    mixins: [UserListMixin],
    components: {
      UserModal,
      PasswordModal,
      UserRoleModal,
      UserPermissionModal,
      JInput
    },
    data() {
      return {
        description: '这是用户管理页面',
        queryParam: {},
        recycleBinVisible: false,
        columns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title: '用户编号',
            align: "center",
            width: 300,
            dataIndex: 'id',
          },
          {
            title: '账号',
            align: "center",
            width: 100,
            dataIndex: 'username',
          },
          {
            title: '姓名',
            align: "center",
            width: 150,
            dataIndex: 'realname'
          },
          {
            title: '电话',
            align: "center",
            width: 200,
            dataIndex: 'phone'
          },
          {
            title: '邮箱',
            align: "center",
            dataIndex: 'email'
          },
          {
            title: '性别',
            align: "center",
            width: 80,
            dataIndex: 'sex',
            scopedSlots: {customRender: 'sexSlot'}
           },
          {
            title: '状态',
            align: "center",
            width: 100,
            dataIndex: 'locked',
            scopedSlots: {customRender: 'lockedSlot'}
          },
          {
            title: '操作',
            fixed: 'right',
            scopedSlots: {customRender: 'actionSlot'},
            align: "center",
            width: 400
          }

        ],
        superQueryFieldList: [
          { type: 'input', value: 'username', text: '用户账号', },
          { type: 'input', value: 'realname', text: '用户姓名', },
          { type: 'select', value: 'sex', text: '性别', dictCode: 'sex' },
        ],
        url: {
          syncUser: "/act/process/extActProcess/doSyncUser",
          list: "/upms/user/list",
          delete: "/upms/user/delete",
          deleteBatch: "/upms/user/deleteBatch",
          exportXlsUrl: "/upms/user/exportXls",
          importExcelUrl: "/upms/user/importExcel",
        },
      }
    },
    mounted () {
      getAction('/upms/user/list').then(res=>{
        console.log('userlist:',res)
      })
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
      handleUserRole(userId){
        this.$refs.modalUserRole.show(userId);
      },
      handleUserPermission(userId){
        this.$refs.userPermissionModal.show(userId);
      },
      handleChangePassword(username) {
        this.$refs.passwordmodal.show(username);
      },
      passwordModalOk() {
        //TODO 密码修改完成 不需要刷新页面，可以把datasource中的数据更新一下
      }
    }

  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>